package cn.hsa.xxx.bookData.bo.impl;

import cn.hsa.xxx.bookData.bo.ShelvesApprovalProcessBO;
import cn.hsa.xxx.bookData.dao.ShelvesApprovalProcessDAO;
import cn.hsa.xxx.api.dto.ShelvesApprovalProcessDTO;
import cn.hsa.xxx.bookData.dto.ShelvesApprovalProcessDTOConverter;
import cn.hsa.xxx.bookData.entity.ShelvesApprovalProcessDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ShelvesApprovalProcessBOImpl implements ShelvesApprovalProcessBO {

    @Resource
    ShelvesApprovalProcessDAO shelvesApprovalProcessDAO;

    @Autowired
    ShelvesApprovalProcessDTOConverter shelvesApprovalProcessDTOConverter;

    @Override
    public ShelvesApprovalProcessDTO add(ShelvesApprovalProcessDTO shelvesApprovalProcessDTO) {
        ShelvesApprovalProcessDO shelvesApprovalProcessDO = shelvesApprovalProcessDTOConverter.convertToShelvesApprovalProcessDO(shelvesApprovalProcessDTO);
        shelvesApprovalProcessDAO.insert(shelvesApprovalProcessDO);
        return shelvesApprovalProcessDTOConverter.convertToShelvesApprovalProcessDTO(shelvesApprovalProcessDO);
    }

    @Override
    public String delete(String id) {
        shelvesApprovalProcessDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public ShelvesApprovalProcessDTO get(String id) {
        ShelvesApprovalProcessDO shelvesApprovalProcessDO = shelvesApprovalProcessDAO.selectById(id);
        ShelvesApprovalProcessDTO shelvesApprovalProcessDTO = shelvesApprovalProcessDTOConverter.convertToShelvesApprovalProcessDTO(shelvesApprovalProcessDO);
        return shelvesApprovalProcessDTO;
    }

    @Override
    public String update(ShelvesApprovalProcessDTO shelvesApprovalProcessDTO) {
        ShelvesApprovalProcessDO shelvesApprovalProcessDO = shelvesApprovalProcessDTOConverter.convertToShelvesApprovalProcessDO(shelvesApprovalProcessDTO);
        shelvesApprovalProcessDAO.updateById(shelvesApprovalProcessDO);
        return "更新成功";
    }

    @Override
    public List<ShelvesApprovalProcessDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ShelvesApprovalProcessDO> shelvesApprovalProcessDOList = shelvesApprovalProcessDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(shelvesApprovalProcessDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<ShelvesApprovalProcessDTO> shelvesApprovalProcessDTOList = new ArrayList<>();
        for (ShelvesApprovalProcessDO shelvesApprovalProcessDO : shelvesApprovalProcessDOList) {
            shelvesApprovalProcessDTOList.add(shelvesApprovalProcessDTOConverter.convertToShelvesApprovalProcessDTO(shelvesApprovalProcessDO));

        }

        return shelvesApprovalProcessDTOList;
    }
}
